// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "order_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "订单编号",
    "label": "订单编号"
  },
  "order_child_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "子订单编号",
    "label": "子订单编号"
  },
  "school_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "所属学校",
    "label": "所属学校"
  },
  "menu_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "所属菜单",
    "label": "所属菜单"
  },
  "yizhan_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "取件地址",
    "label": "取件地址"
  },
  "userAddress_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "用户地址",
    "label": "用户地址"
  },
  "user_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "创建用户",
    "label": "创建用户"
  },
  "user_address_text": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 800
      }
    ],
    "title": "完整送货地址",
    "label": "完整送货地址"
  },
  "user_remark": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      },
      {
        "maxLength": 500
      }
    ],
    "title": "订单备注",
    "label": "订单备注"
  },
  "expectedDelivery_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "预计送达日期",
    "label": "预计送达日期"
  },
  "expectedDelivery_min": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "最小时间",
    "label": "最小时间"
  },
  "expectedDelivery_max": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "最大时间",
    "label": "最大时间"
  },
  "expectedDelivery_text": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "预计送达时间",
    "label": "预计送达时间"
  },
  "order_end_time": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "title": "订单结束时间",
    "label": "订单结束时间"
  },
  "order_out_time": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "title": "订单超时时间",
    "label": "订单超时时间"
  },
  "order_tips_money": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "订单小费",
    "label": "订单小费"
  },
  "order_all_money": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "订单总金额",
    "label": "订单总金额"
  },
  "order_connect_phone": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "快递手机号",
    "label": "快递手机号"
  },
  "pack_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "包裹类型",
    "label": "包裹类型"
  },
  "verify_code": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "取件码",
    "label": "取件码"
  },
  "fast_status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "bool"
      }
    ],
    "title": "是否加急",
    "defaultValue": false,
    "label": "是否加急"
  },
  "rider_order_get_status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "bool"
      }
    ],
    "title": "是否接单",
    "defaultValue": false,
    "label": "是否接单"
  },
  "rider_order_get_time": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "timestamp"
      }
    ],
    "title": "接单时间",
    "label": "接单时间"
  },
  "rider_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "接单骑手",
    "label": "接单骑手"
  },
  "rider_order_status": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "int"
      },
      {
        "range": [
          {
            "value": 0,
            "text": "待接单"
          },
          {
            "value": 1,
            "text": "已接单"
          },
          {
            "value": 2,
            "text": "已取货"
          },
          {
            "value": 3,
            "text": "派送中"
          },
          {
            "value": 4,
            "text": "已完成"
          },
          {
            "value": 5,
            "text": "已失败"
          },
          {
            "value": 6,
            "text": "已取消"
          },
          {
            "value": 7,
            "text": "已超时"
          },
          {
            "value": 8,
            "text": "待付款"
          },
          {
            "value": 9,
            "text": "退款/售后"
          },
          {
            "value": 10,
            "text": "支付超时，订单取消"
          }
        ]
      }
    ],
    "title": "订单状态",
    "defaultValue": 0,
    "label": "订单状态"
  },
  "rider_order_receipt": {
    "rules": [
      {
        "format": "array"
      },
      {
        "arrayType": "file"
      },
      {
        "maxLength": 3
      }
    ],
    "title": "订单完成回执",
    "label": "订单完成回执"
  },
  "three_pay_order_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "发送给第三方订单的编号",
    "label": "发送给第三方订单的编号"
  },
  "three_pay_transaction_id": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "第三方订单支付状态回调的编号",
    "label": "第三方订单支付状态回调的编号"
  }
}

const enumConverter = {
  "rider_order_status_valuetotext": {
    "0": "待接单",
    "1": "已接单",
    "2": "已取货",
    "3": "派送中",
    "4": "已完成",
    "5": "已失败",
    "6": "已取消",
    "7": "已超时",
    "8": "待付款",
    "9": "退款/售后",
    "10": "支付超时，订单取消"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
